define(["vue","text!tasksHtml","jquery"],function(vue,html,$){

    return vue.extend({
        template: html,
        data:function(){
            return {
                taskTimer: 0,
                dialogFormVisible:false,
                formLabelWidth: '80px',
                form:{
                    name:'',
                    url:'',
                    path:'',
                    version:'',
                    group:'',
                    method:'',
                    parameters:'',
                    cron:'',
                    protocol:''
                },
                rules:{
                    name:[
                        { required: true, message: '请输入名称', trigger: 'blur' },
                        { min: 3, max: 30, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    url:[
                        { required: true, message: '注册路径不能为空', trigger: 'blur' }
                    ]
                },
                tableData: [],
                showArray:{
                    urlShow:false,
                    versionShow:false,
                    groupShow:false
                },
                nameArray:{
                    groupName:'分组',
                }
            };
        },
        created:function(){
            var taskComp = this;
            $.ajax({
                url:"/scheduling/list",
                type:"get",
                dataType:"json",
                success:function(data){
                    if(data.code == '0'){
                        taskComp.tableData = data.data;
                    }else{
                        taskComp.$options.methods.alertInfo('error',data.data,taskComp);
                    }
                }
            });
            taskComp.taskTimer = setInterval(function(){
                $.ajax({
                    url:"/scheduling/errorInfo",
                    type:"get",
                    dataType:"json",
                    success:function(data){
                        if(data.code == '0'){
                            var errorList = data.data;
                            if(errorList.length>0){
                                for(var i = 0;i<errorList.length;i++){
                                    var errorData = errorList[i];
                                    taskComp.$options.methods.errorNofity(taskComp,errorData.name+"执行异常！");
                                }
                            }
                        }
                    }
                });

            },1000);
        },
        beforeDestroy:function(){
            clearInterval(this.taskTimer);
        },
        methods:{
            addEvent(){
                var taskComp = this;
                $.ajax({
                    url:"/scheduling/add",
                    type:"get",
                    dataType:"json",
                    data:taskComp.form,
                    success:function(data){
                        if(data.code == '0'){
                            taskComp.dialogFormVisible=false;
                            taskComp.$options.methods.alertInfo('success','添加成功！',taskComp);
                            setTimeout(function(){
                                location.reload();//刷新页面
                            });
                        }else{
                            taskComp.$options.methods.alertInfo('error',data.data,taskComp);
                        }
                    }
                });
            },
            showEvent(name){
                /**
                 * query和params区别
                 * query 请求方式 如 /show?name=1
                 * params 请求方式如 /show/:name
                 * */
                this.$router.push({path: '/show',query: {name:name}});
            },
            delEvent(name){
                var thisObj = this;
                $.ajax({
                    url:"/scheduling/delete",
                    type:"get",
                    dataType:"json",
                    data:{name:name},
                    success:function(data){
                        if(data.code == '0'){
                            thisObj.$options.methods.alertInfo('success','删除成功',thisObj);
                            setTimeout(function(){
                                location.reload();//刷新页面
                            },1000)
                        }else{
                            thisObj.$options.methods.alertInfo('error',data.data,thisObj);
                        }
                    }
                });
            },
            changeState(name,state){
                var thisObj = this;
                $.ajax({
                    url:"/scheduling/changeSate",
                    type:"get",
                    dataType:"json",
                    data:{name:name,state:state},
                    success:function(data){
                        if(data.code == '0'){
                            thisObj.$options.methods.alertInfo('success','修改成功',thisObj);
                            setTimeout(function(){
                                location.reload();//刷新页面
                            },1000);
                        }else{
                            thisObj.$options.methods.alertInfo('error',data.data,thisObj);
                        }
                    }
                });
            },
            alertInfo(type,message,thisObj){
                thisObj.$message({
                    showClose: true,
                    message: message,
                    type: type
                });
            },
            tableRowClassName({row, rowIndex}) {
                if (row.state === 1) {
                    return 'warning-row';
                }
                return '';
            },
            errorNofity(thisObj,msg){
                thisObj.$notify.error({
                    title: '错误',
                    message: msg
                });
            },
            /** 协议事件 */
            protoclEvent(value){
                if(value == 'dubbo'){
                    this.showArray.urlShow = true;
                    this.showArray.versionShow = true;
                    this.showArray.groupShow = true;
                }else if(value == 'http'){
                    this.showArray.urlShow = false;
                    this.showArray.versionShow = false;
                    this.showArray.groupShow = true;
                }else if(value == 'local'){
                    this.showArray.urlShow = false;
                    this.showArray.versionShow = false;
                    this.showArray.groupShow = false;
                }
            }
        }
    });
})